package cn.edu.gcu.lost.sys.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.RuntimeSqlException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.gcu.lost.sys.dao.RoleDao;
import cn.edu.gcu.lost.sys.dao.UserDao;
import cn.edu.gcu.lost.sys.entity.Menu;
import cn.edu.gcu.lost.sys.entity.Role;
import cn.edu.gcu.lost.sys.entity.User;
import cn.edu.gcu.lost.sys.util.UserUtils;

@Service
@Transactional(readOnly = true)
public class RoleService {

    private static int NO_CHANGE = 0;
    private static int RESULT_NO_ERROR = 0;
    private static int RESULT_HAS_ERROR = 1;

    @Autowired
    private RoleDao roleDao;
    @Autowired
    private UserDao userDao;

    public Object findList() {
        return roleDao.findAllList(new Role());
    }

    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public boolean save(Role role) {
        int result = RESULT_NO_ERROR;
        List<Menu> menuList = null;
        if (StringUtils.isBlank(role.getId())) {
            role.preInsert();
            if (roleDao.insert(role) == NO_CHANGE)
                return false;
        } else {
            role.preUpdate();
            if (roleDao.update(role) == NO_CHANGE)
                return false;
            roleDao.removeAllMenu(role);
            List<User> userList = userDao.findListByRole(new User(role));
            for (User user : userList) {
                UserUtils.clearCache(user);
            }
        }
        menuList = role.getMenuList();
        if (menuList != null) {
            for (Menu menu : menuList) {
                if (roleDao.addMenu(role, menu) == NO_CHANGE) {
                    result = RESULT_HAS_ERROR;
                    break;
                }
            }
        }
        if (result == RESULT_HAS_ERROR) {
            throw new RuntimeSqlException("修改菜单权限异常");
        }
        return true;
    }

    @Transactional(readOnly = false)
    public boolean delete(Role role) {
        if (roleDao.isExist(role) == 0) {
            return false;
        } else if (roleDao.delete(role) == NO_CHANGE) {
            return false;
        } else {
            roleDao.removeAllMenu(role);
            List<User> userList = userDao.findListByRole(new User(role));
            for (User user : userList) {
                UserUtils.clearCache(user);
            }
            return true;
        }
    }

    public boolean isUseable(Role role) {
        String useable = roleDao.queryUseable(role);
        if (useable == null || useable.equals(role.getUseable())) {
            return false;
        } else {
            return roleDao.isUseable(role) > 0;
        }
    }

    public Role findDetail(Role role) {
        return roleDao.findDetail(role);
    }

}
